<template>
	<view class="u-page">
		<view class="u-demo-block">
			<u-form
				labelPosition="left" 
				:labelWidth="80" 
				:model="formData" 
				:rules="formRules" 
				ref="form">
				<u-form-item
					label="账户号"
					required
					prop="userInfo.userCode"
					ref="userCode">
					<u-input
						v-model="formData.userInfo.userCode"
						:disabled="true">
					</u-input>				
				</u-form-item>
				<!-- 输入设备 -->
				<u-form-item
					label="设备号"
					required
					prop="machineInfo.machineNo"
					ref="machineNo">
					<u-input
						v-model="formData.machineInfo.machineNo"
						:disabled="true">
					</u-input>				
				</u-form-item>
				<u-form-item
					label="日期轮班"
					prop="recordTimeDT"
					ref="recordTimeDT"
					required>
					<u-input
						v-model="formData.recordTimeDT"
						disabled
						placeholder="请选择日期轮班"
						fontSize="13px"
						color="#12569F">
					</u-input>
				</u-form-item>
				<!-- 计划产量 -->
				<u-form-item
					label="计划产量"
					required
					prop="planOutput"
					ref="planOutput">
					<u-input
						v-model="formData.planOutput"
						type="number"
						placeholder="请输入计划产量"
						clearable>
						<u--text slot="suffix" type="info" text="kpcs"></u--text>
					</u-input>				
				</u-form-item>
				<!-- 实际产量 -->
				<u-form-item
					label="实际产量"
					required
					prop="actualOutput"
					ref="actualOutput">
					<u-input
						v-model="formData.actualOutput"
						type="number"
						placeholder="请输入实际产量"
						clearable>
						<u--text slot="suffix" type="info" text="kpcs"></u--text>
					</u-input>				
				</u-form-item>
				<!-- 计划产量 -->
				<u-form-item
					label="不良数"
					required
					prop="defect"
					ref="defect">
					<u-input
						v-model="formData.defect"
						type="number"
						placeholder="请输入不良数"
						clearable>
						<u--text slot="suffix" type="info" text="pcs"></u--text>
					</u-input>				
				</u-form-item>
				<!-- 输入备注 -->
				<u-form-item
					label="备注"
					prop="remark"
					ref="remark">
					<u--textarea
						v-model="formData.remark"
						placeholder="按需输入"
						autoHeight
						:count="true"
						:maxlength="144">
					</u--textarea>
				</u-form-item>
			</u-form>	
			<!-- 按钮 -->
			<u-button
				type="primary"
				:text="submitBtn.text"
				customStyle="margin-top: 20px"
				:loading="submitBtn.loading"
				:disabled="submitBtn.disabled"
				loadingMode="semicircle"
				@click="formEditSubmit">
			</u-button>
			<u-button
				type="error"
				:text="delBtn.text"
				customStyle="margin-top: 20px"
				:loading="delBtn.loading"
				:disabled="delBtn.disabled"
				loadingMode="semicircle"
				@click="formDelSubmit">
			</u-button>
	
			

		</view>
	</view>
</template>

<script>
	import {getShiftCapacityRecordByID,uptShiftCapacityRecord,delShiftCapacityRecord} from '@/api/shiftcaparecord/shiftcaparecord.js'
	export default {
		data() {
			return {
				prePageOption:null,  // 上一页传过来的参数
				formData:{
					userInfo:{
						userCode:''
					},
					machineInfo:{
						machineNo:''
					},
					recordTimeDT:'',
					planOutput:null,
					actualOutput:null,
					defect:null,
					recordRemark:''
				},
				// 校验数据
				formRules:{
					'userInfo.userCode':[
						{
							type: 'string',
							required: true,
							message: '请填写姓名',
							trigger: ['blur', 'change'],
						},
					],
					'machineInfo.machineNo':[
						{
							type: 'string',
							required: true,
							message: '请填写设备号',
							trigger: ['blur', 'change']
						},					
					],
					recordTimeDT:[
						{
							type: 'string',
							required: true,
							message: '不能为空',
							trigger: ['blur', 'change'],
						},
					],
					planOutput:[
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								if(uni.$u.test.isEmpty(value)){
									return false
								}
								if(Number(value)<0){
									return false
								}
								return true;
							},
							message:'必填且≥0',
							trigger:['change','blur']
						}
					],
					actualOutput:[
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								if(uni.$u.test.isEmpty(value)){
									return false
								}
								if(Number(value)<0){
									return false
								}
								return true;
							},
							message:'必填且≥0',
							trigger:['change','blur']
						}
					],
					defect:[
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// if(uni.$u.test.isEmpty(value)){
								// 	return false
								// }
								if(Number(value)<0){
									return false
								}
								return true;
							},
							message:'必填且≥0',
							trigger:['change','blur']
						}
					]
				},
				//
				submitBtn:{
					text:'提交修改',
					loading:false,
					disabled:false
				},
				delBtn:{
					text:'删除',
					loading:false,
					disabled:false
				}
				
				
			}
		},
		onLoad(option) {
			this.prePageOption=option
		},
		onReady(){
			const _this=this
			this.$nextTick(()=>{
				getShiftCapacityRecordByID({shiftCapaRecordID:this.prePageOption.shiftCapaRecordID}).then(res=>{
					// console.log(res);
					_this.formData.userInfo.userCode=res.data.userCode
					_this.formData.machineInfo.machineNo=res.data.machineNo
					_this.formData.planOutput=res.data.planOutput
					_this.formData.actualOutput=res.data.actualOutput
					_this.formData.defect=res.data.defect
					_this.formData.remark=res.data.recordRemark||''
					// 时间需要判断显示
					_this.formData.recordTimeDT = res.data.recordTimeDT
					// let utcTimeOffset2 = new Date().getTimezoneOffset()
					// if(utcTimeOffset2!=(-480) && !uni.$u.test.isEmpty(_this.formData.recordTime)){
					// 	_this.formData.recordTime=_this.formData.recordTime-(utcTimeOffset2-480)*60000
					// }
					
					/*
					// 换算成1值
					if(new Date(_this.formData.recordTimeDT).getHours().toString().includes('20')){
						_this.formData.recordTimeDT=uni.$u.timeFormat(_this.formData.recordTimeDT,'yyyy-mm-dd')+' 1值' 
					} 
					//换算成2值
					else if (new Date(_this.formData.recordTimeDT).getHours().toString().includes('8')){ 
						_this.formData.recordTimeDT=uni.$u.timeFormat(dayjs(_this.formData.recordTimeDT).add(-1,'day'),'yyyy-mm-dd')+' 2值'
					} else {
						_this.formData.recordTimeDT=uni.$u.timeFormat(_this.formData.recordTimeDT,'yyyy-mm-dd hh点')
					} 
					*/
					_this.formData.recordTimeDT=uni.$u.timeFormat(_this.formData.recordTimeDT,'yyyy-mm-dd hh点')
				}).catch(err=>{
					uni.showToast({
						title:err.msg|| '获取数据异常',
						icon:'error',
						duration:2000
					})
					uni.$u.sleep(2000).then(()=>{
						uni.navigateBack();
					})
				})
			})
		},
		onNavigationBarButtonTap(e) {
			if (e.type === 'menu') {
				// this.showMenuDrawer();
			} else if (e.type === 'home') {
				uni.reLaunch({
					url: '/pages/index/index'
				});
			}
		},
		methods:{
			formEditSubmit(){
				const _this= this

				this.$refs.form.validate().then(res=>{
					uni.showModal({
						title:'确认修改',
						content:'确认修改数据吗 ?',
						success: (res) => {
							if(res.confirm){
								let tmpdata={
									shiftCapaRecordID:_this.prePageOption.shiftCapaRecordID,
									planOutput:_this.formData.planOutput,
									actualOutput: _this.formData.actualOutput,
									defect: _this.formData.defect,
									remark: _this.formData.remark
								}
								uptShiftCapacityRecord(tmpdata).then(res=>{
									uni.showToast({
										title:'提交成功',
										duration:2000,
									})
									uni.$u.sleep(2000).then(()=>{
										uni.redirectTo({
											url:'/pages/index/index'
										})
									})
								}).catch(err=>{
									uni.showToast({
										title: err.msg||' 服务器异常',
										icon:'error',
										duration:2000,
									})
								}).finally(()=>{
									_this.submitBtn.text="提交"
									_this.submitBtn.disabled=false
									_this.submitBtn.loading=false
								})
								
							}
						}
						
					})
				}).catch(err=>{
					uni.showToast({
						title:'提交数据错误',
						icon:'error',
						duration:2000
					})
				})
			},
			formDelSubmit(){
				const _this= this

				this.$refs.form.validate().then(res=>{
					uni.showModal({
						title:'确认修改',
						content:'确认修改数据吗 ?',
						success: (res) => {
							if(res.confirm){
								let tmpdata={
									shiftCapaRecordID:_this.prePageOption.shiftCapaRecordID,
								}
								delShiftCapacityRecord(tmpdata).then(res=>{
									uni.showToast({
										title:'删除成功',
										duration:2000,
									})
									uni.$u.sleep(2000).then(()=>{
										uni.redirectTo({
											url:'/pages/index/index'
										})
									})
								}).catch(err=>{
									uni.showToast({
										title: err.msg||' 服务器异常',
										icon:'error',
										duration:2000,
									})
								}).finally(()=>{
									_this.submitBtn.text="提交"
									_this.submitBtn.disabled=false
									_this.submitBtn.loading=false
								})
								
							}
						}
						
					})
				}).catch(err=>{
					uni.showToast({
						title:'提交数据错误',
						icon:'error',
						duration:2000
					})
				})
			}
		}
		
		
		
	}
</script>

<style lang="scss">

</style>
